package com.oauth.global.server.service;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Required;

import com.oauth.global.client.beans.AppInfoBean;
import com.oauth.global.client.beans.BirthdayClientBean;
import com.oauth.global.server.utils.GlobalUtil;
import com.oauth.service.application.ApplicationService;
import com.oauth.service.application.global.GlobalService;
import com.oauth.service.application.global.beans.Birthday;
import com.oauth.service.application.global.beans.GlobalDataType;
import com.oauth.service.application.registry.beans.ApplicationInfoBean;

public class GlobalOAuthServiceImpl implements GlobalOAuthService {

	private GlobalService mGlobalService;

	private ApplicationService mApplicationService;

	/**
	 * @param pGlobalService
	 *            the globalService to set
	 */
	@Required
	public void setGlobalService(GlobalService pGlobalService) {
		mGlobalService = pGlobalService;
	}

	/**
	 * @param pApplicationService
	 *            the applicationService to set
	 */
	@Required
	public void setApplicationService(ApplicationService pApplicationService) {
		mApplicationService = pApplicationService;
	}

	@Override
	public boolean updateStatus(Long pMemberId, String pStatusText, Set<Long> pApplicationIdSet) {
		return mGlobalService.updateStatus(pMemberId, pApplicationIdSet, pStatusText);
	}

	@Override
	public List<BirthdayClientBean> getUpcomingBirthdays(Long pMemberId) {
		Set<Long> applicationIdSet = getApplicationIdSet(pMemberId,
				GlobalDataType.BIRTHDAY_NOTIFACTION);
		List<Birthday> birthdayList = mGlobalService.getUpcomingBirthdays(pMemberId,
				applicationIdSet);
		return GlobalUtil.convertToBirthdayList(birthdayList);
	}

	@Override
	public List<BirthdayClientBean> getUpcomingBirthdays(Long pMemberId, long pAppId) {
		Set<Long> applicationIdSet = new HashSet<Long>(1);
		applicationIdSet.add(pAppId);
		List<Birthday> birthdayList = mGlobalService.getUpcomingBirthdays(pMemberId,
				applicationIdSet);
		return GlobalUtil.convertToBirthdayList(birthdayList);
	}

	@Override
	public List<AppInfoBean> getApplicationImages(Long pMemberId, GlobalDataType pGlobalDataType) {
		Set<Long> applicationIdSet = getApplicationIdSet(pMemberId, pGlobalDataType);
		Collection<ApplicationInfoBean> applicationInfo = mApplicationService
				.getApplicationInfo(applicationIdSet);
		return GlobalUtil.convertToClientList(applicationInfo);
	}

	private Set<Long> getApplicationIdSet(Long pMemberId, GlobalDataType pGlobalDataType) {
		return mApplicationService.getApplicationIdSetWithDataTypeForMember(pMemberId,
				pGlobalDataType);
	}

}
